package org.baicaixiaozhan.emailmanager.vo;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Set;
import java.util.stream.Collectors;


/**
 * DESC: 用户权限
 *
 * @author : baicaixiaozhan
 * @since : 2021-06-20
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class UserRole {
    private String role;
    private Set<UserPermission> permissions;

    /**
     * 获取一组 赋予权限的 stores (simple)
     */
    public Set<SimpleGrantedAuthority> getGrantedAuthorities() {
        Set<SimpleGrantedAuthority> authorities = getPermissions().stream()
                .map(permission -> new SimpleGrantedAuthority(permission.getPermission()))
                .collect(Collectors.toSet());
        authorities.add(new SimpleGrantedAuthority("ROLE_" + getRole()));
        return authorities;
    }
}
